package Controller.servlet;

import DAO.DoForUser;
import Domain.UserBean;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.Map;

@WebServlet("/register")
public class RegisterServlet extends HttpServlet {

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        UserBean user = new UserBean();
        Map<String, String[]> parameterMap = req.getParameterMap();
        try {
            BeanUtils.populate(user,parameterMap);
            System.out.println(user.getUser_name()+user.getUser_password());
            boolean b = DoForUser.doRegister(user);
            if(b){
                req.getRequestDispatcher("/ReSuccess").forward(req,resp);

            }else {
                req.getRequestDispatcher("/ReFail").forward(req,resp);

            }
        } catch (IllegalAccessException e) {
            e.printStackTrace();
            System.out.println("access出错了");
        } catch (InvocationTargetException e) {
            System.out.println("tar出错了！！！");
            e.printStackTrace();
        }

    }
}
